package com.winhxd.b2c.customer.service.impl;

import com.winhxd.b2c.common.domain.customer.condition.InvitationRecordCondition;
import com.winhxd.b2c.common.domain.customer.model.CustomerInvitationRecord;
import com.winhxd.b2c.common.domain.customer.vo.InvitationRecordVO;
import com.winhxd.b2c.customer.dao.CustomerInvitationRecordMapper;
import com.winhxd.b2c.customer.service.InvitationRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author baishenghu
 */
@Service
public class InvitationRecordServiceImpl implements InvitationRecordService {

    private static final Logger logger = LoggerFactory.getLogger(InvitationRecordServiceImpl.class);

    @Autowired
    private CustomerInvitationRecordMapper invitationRecordMapper;

    @Override
    public void saveInvitationRecord(CustomerInvitationRecord invitationRecord) {
        invitationRecord.setInvitationTime(new Date());
        InvitationRecordCondition condition = new InvitationRecordCondition();
        condition.setSlaveId(invitationRecord.getSlaveId());
        CustomerInvitationRecord vo = invitationRecordMapper.selectByMasterAndSlave(condition);
        if(null==vo) {
            //如果没有被邀请在保存
            condition.setMasterId(invitationRecord.getMasterId());
            vo = invitationRecordMapper.selectByMasterAndSlave(condition);
            if(null==vo) {
                //不存在在保存
                invitationRecordMapper.insertSelective(invitationRecord);
            }
        }else{
            logger.info("邀请关系保存已经存在,邀请人{},被邀请人{}",invitationRecord.getMasterId(),invitationRecord.getSlaveId());
        }
    }

    @Override
    public CustomerInvitationRecord getInvitationRecord(InvitationRecordCondition condition) {
        return invitationRecordMapper.selectByMasterAndSlave(condition);
    }

    @Override
    public List<CustomerInvitationRecord> findInvitationRecord(InvitationRecordCondition condition) {
        return invitationRecordMapper.selectByMasterId(condition);
    }
}
